﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DigitalRubyShared;
public class LevelFGRotate : MonoBehaviour
{
    public GameObject targetObject;
    private RotateGestureRecognizer rotateGesture;
    public GameLevelItem gameLevelItem;
    private bool isRot90 = false;
    private void OnEnable()
    {
        rotateGesture = new RotateGestureRecognizer();
        rotateGesture.StateUpdated += RotateGestureCallback;
        FingersScript.Instance.AddGesture(rotateGesture);
    }

    private void OnDisable()
    {
        if (FingersScript.HasInstance)
        {
            FingersScript.Instance.RemoveGesture(rotateGesture);
        }
    }

    private void RotateGestureCallback(DigitalRubyShared.GestureRecognizer gesture)
    {
        if (isRot90)
            return;
        Debug.Log(gesture.State.ToString());
        if (gesture.State == GestureRecognizerState.Executing)
        {
            targetObject.transform.Rotate(0.0f, 0.0f, rotateGesture.RotationRadiansDelta * Mathf.Rad2Deg);
        }

        if(Mathf.Abs(CheckAngle(targetObject.transform.rotation.eulerAngles.z))>=90)
        {
            isRot90 = true;
            gameLevelItem.TriggerResult(true);
        }
    }

    public float CheckAngle(float angleValue)
    {
        float angle = angleValue - 180;
        if (angle > 0)
        {
            return angle - 180;
        }

        if (angleValue == 0)
        {
            return 0;
        }

        return angle + 180;
    }

}
